package com.zr.service.intelligent.impl;

import com.zr.dao.base.BaseCityDao;
import com.zr.dao.intelligent.IntelligentTransferStationDao;
import com.zr.service.intelligent.DijkstraService;
import com.zr.utils.DijkstraUtil;
import com.zr.utils.EpidemicSituationUtil;
import com.zr.vo.base.BaseCity;
import com.zr.vo.intelligent.IntelligentTransferStation;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 杜雨萌
 * @date 2022/10/29
 * @version 2022.1
 */
@Service
public class DijkstraServiceImpl implements DijkstraService {

    @Resource
    private IntelligentTransferStationDao intelligentTransferStationDao;

    @Resource
    private BaseCityDao baseCityDao;

    /**
     * 计算最短路径
     * @param intelligentTransferStation
     * @return map包含三个对象，分别是最短路径list，中转站list，疫情城市map
     * @throws IOException
     */
    @Override
    public  Map<String,Object> shortPath(IntelligentTransferStation intelligentTransferStation) throws IOException {

        //要返回的结果map，其中包括三个list，一是最短路径的城市对象，二是不包括疫情严重城市的中转站城市对象，三是疫情城市的中转站城市对象
        Map<String,Object> resultMap = new HashMap<>();
        //获得所有中转站城市对象
        List<IntelligentTransferStation> transList = intelligentTransferStationDao.list();
        //所有的城市集合，包括前端输入的起点城市、中转站城市和终点城市，用于计算最短路径
        List<String> cityList = new ArrayList<>();
        //添加起点城市
        cityList.add(intelligentTransferStation.getDeparture());
        //添加中转站中非起点和非终点城市
        for(int i = 0;i<transList.size();i++){
            if(!transList.get(i).getCity().equals(intelligentTransferStation.getDeparture()) && !transList.get(i).getCity().equals(intelligentTransferStation.getDestination())){
                cityList.add(transList.get(i).getCity());
            }
        }
        //添加终点城市
        cityList.add(intelligentTransferStation.getDestination());
        //计算最短路径获得结果集
        List<String> resultList = DijkstraUtil.shortPath(cityList);
        //前端需要最短路径城市的经纬度,将string转换为BaseCity对象，获得最短路径城市对象
        List<BaseCity> shortPathCityList = new ArrayList<>();
        for(int i = 0; i<resultList.size();i++){
            List<BaseCity> shortPathCity = baseCityDao.selectByCity(resultList.get(i));
            shortPathCityList.add(shortPathCity.get(0));
        }
        //获得疫情城市信息
        List<Map<String,Object>> specialCityList = new ArrayList<>();
        for(int i = 0;i<transList.size();i++){
            Map<String,Object> specialCityMap = EpidemicSituationUtil.getSpecialCityList(transList.get(i).getCity());
            if(specialCityMap.size() != 0){
                specialCityList.add(specialCityMap);
            }
        }
        //得到不包含起点、终点、疫情城市的中转站城市列表
        List<BaseCity> transCityList = new ArrayList<>();
        for(int i = 0;i<transList.size();i++){
            String currentCity = transList.get(i).getCity();
            if(!currentCity.equals(intelligentTransferStation.getDeparture()) && !currentCity.equals(intelligentTransferStation.getDestination())){
                boolean flag = true;
                for(int j =0;j<specialCityList.size();j++){
                    if(currentCity.equals((String) specialCityList.get(j).get("city"))){
                        flag = false;
                    }
                }
                if(flag){
                    transCityList.add(baseCityDao.selectByCity(currentCity).get(0));
                }
            }
        }
        resultMap.put("shortPathCityList",shortPathCityList);
        resultMap.put("transCityList",transCityList);
        resultMap.put("specialCityList",specialCityList);
        return resultMap;
    }

}
